<?php
/**
 * @Author: Nick
 * @Date:   2015-01-19 10:39:19
 * @Last Modified by:   Nick
 * @Last Modified time: 2015-01-19 15:06:22
 */
class ListController extends Controller{
	
	/**
	 * 列表页
	 */
	public function index(){
		//顶级分类
		$topCate = M('category')->where(array('pid'=>0))->all();
		$this->assign('topCate',$topCate);
		//获得分类相对应的商品id
		$cid = Q('get.cid');
		//获得所有子集cid
		$cids = $this->_get_son_id($cid);
		//获得分类所对应的所有商品id
		$gids = M('goods')->where(array('cid in (' . implode(',', $cids) . ')'))->getField('gid',true);
		//如果分类下面有商品
		if($gids){
			$attr = M('goods_attr')->where(array('gid in (' . implode(',', $gids) . ')'))->group('gtvalue')->all();
			
			$attrs = array();
			foreach ($attr as $k => $v) {
				$attrs[$v['taid']][] = $v;
			}
			

			$attrFinal = array();
			$model = M('type_attr');
			foreach ($attrs as $k => $v) {
				$attrFinal[] = array(
					'taname'	=> $model->where(array('taid'=>$k))->getField('taname'),
					'tavalue'	=>	$v

					);
			}

			$this->assign('attrFinal',$attrFinal);
		}


		$this->display();
	}


	private function _get_son_id($cid){
		$data = M('category')->all();
		$cids = $this->_get_son($data,$cid);
		$cids[] = $cid;
		return $cids;
	}

	private function _get_son($data,$cid){
		$temp = array();
		foreach ($data as $k => $v) {
			if($v['pid'] == $cid){
				$temp[] = $v['cid'];
				$temp = array_merge($temp,$this->_get_son($data,$v['cid']));
			}
		}

		return $temp;
	}

}